<template>
  <div>
    <h1>学生信息查询</h1>
    <input v-model="studentId" placeholder="请输入学号" @keyup.enter="searchStudent">
    <button @click="searchStudent">查询</button>
    <div v-if="studentInfo">
      <h3>学生信息：</h3>
      <p>学号: {{ studentInfo.id }}</p>
      <p>姓名: {{ studentInfo.name }}</p>
    </div>
    <div v-if="errorMessage" class="error-message">
      {{ errorMessage }}
    </div>
  </div>
</template>

<script>
export default {
  name: 'CampusBinhai',
  data() {
    return {
      studentId: '',
      studentInfo: null,
      errorMessage: '',
      // 学生数据库
      students: [
        { id: '123456', name: '陈冕' },
        { id: '654321', name: '陈丁' },
        { id: '2023001', name: '张三' },
        { id: '2023002', name: '李四' },
        { id: '2023003', name: '王五' }
      ]
    }
  },
  methods: {
    searchStudent() {
      // 清空之前的结果
      this.studentInfo = null;
      this.errorMessage = '';
      
      if (!this.studentId) {
        this.errorMessage = '请输入学号';
        return;
      }
      
      // 查找匹配的学生
      const foundStudent = this.students.find(student => 
        student.id === this.studentId
      );
      
      if (foundStudent) {
        this.studentInfo = foundStudent;
      } else {
        this.errorMessage = '未找到该学号对应的学生';
      }
    }
  }
}
</script>

<style scoped>
.error-message {
  color: red;
  margin-top: 10px;
}

input {
  padding: 8px;
  margin-right: 10px;
}

button {
  padding: 8px 16px;
  background-color: #42b983;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

button:hover {
  background-color: #3aa876;
}
</style>